package com.atguigu.demo.handler;

import com.atguigu.demo.entity.Movie;
import com.atguigu.demo.service.MovieService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class MovieHandler {

    @Autowired
    private MovieService movieService;

    @RequestMapping(value = "/movie", method = RequestMethod.GET)
    public String getMovieList(Model model) {

        List<Movie> movieList = movieService.getAll();

        model.addAttribute("movieList", movieList);

        return "movie-list";
    }

    @RequestMapping(value = "/movie/{movieId}", method = RequestMethod.DELETE)
    public String removeMovie(@PathVariable("movieId") String movieId) {

        movieService.removeMovieById(movieId);

        return "redirect:/movie";
    }

    @RequestMapping(value = "/movie", method = RequestMethod.POST)
    public String saveMovie(Movie movie) {

        movieService.saveMovie(movie);

        return "redirect:/movie";
    }

    @RequestMapping(value = "/movie/{movieId}", method = RequestMethod.GET)
    public String toEditPage(@PathVariable("movieId") String movieId, Model model) {

        Movie movie = movieService.getMovieById(movieId);

        model.addAttribute("movie", movie);

        return "movie-edit";
    }

    @RequestMapping(value = "/movie", method = RequestMethod.PUT)
    public String updateMovie(Movie movie) {

        movieService.updateMovie(movie);

        return "redirect:/movie";
    }

}
